/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sais.eswn.ui;

import java.io.InputStream;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import org.sais.eswn.tool.ReadInnerFile;
import org.sais.eswn.tool.Tools;

/**
 *
 * @author Romulus
 */
public class SoundManager {
    public static void playSound(String filename) {
        try {
            InputStream in = new ReadInnerFile(filename).u.openStream();
            AudioInputStream stream = AudioSystem.getAudioInputStream(in);
            AudioFormat format = stream.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, format);
            final Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(stream);

            clip.addLineListener(new LineListener() {
                @Override
                public void update(LineEvent event) {
                    if (event.getType() == LineEvent.Type.STOP) {
                        clip.stop();
                        clip.close();
                    }
                }
            });

            clip.start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
